﻿using System;
using System.Collections.Generic;

namespace Comsenz.Services.Blocks.Configs
{
    /// <summary>
    /// 模块配置
    /// </summary>
    /// <author>Chiron</author>
    public class BlockConfig : IConfig
    {
        #region Ctors
        /// <summary>
        /// 构建模块配置
        /// </summary>
        public BlockConfig()
        {

        }
        /// <summary>
        /// 构建模块配置
        /// </summary>
        /// <param name="name">模块名</param>
        /// <param name="fields">可显示的字段</param>
        /// <param name="settings">可设置和接收的参数</param>
        /// <param name="styles">内置的显示样式</param>
        public BlockConfig(string name, BlockFields fields = null, BlockGetSettings settings = null, BlockStyles styles = null)
        {
            Name = name;
            Fields = fields;
            GetSettings = settings;
            Styles = styles;
        }
        #endregion Ctors


        #region Props
        private string version = "X2";
        /// <summary>
        /// 版本号
        /// </summary>
        public string Version
        {
            get
            {
                return version;
            }
            set
            {
                if (string.IsNullOrEmpty(value)) throw new ArgumentNullException("version");
                version = value;
            }
        }

        private string name;
        /// <summary>
        /// 模块名
        /// </summary>
        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                if (string.IsNullOrEmpty(value)) throw new ArgumentNullException("name");
                name = value;
            }
        }
        /// <summary>
        /// 可显示的字段
        /// </summary>
        public BlockFields Fields { get; set; }
        /// <summary>
        /// 可设置和接收的参数
        /// </summary>
        public BlockGetSettings GetSettings { get; set; }

        /// <summary>
        /// 内置的显示样式
        /// </summary>
        public BlockStyles Styles { get; set; }
        #endregion Props

        #region Methods
        /// <summary>
        /// 取得配置值
        /// </summary>
        /// <returns></returns>
        public Dictionary<object, object> GetConfigValue()
        {
            var fields = new Dictionary<object, object>();
            if (Fields != null)
            {
                foreach (var f in Fields)
                {
                    fields.Add(f.Key, f.Value.GetConfigValue());
                }
            }

            var getsettings = new Dictionary<object, object>();
            if (GetSettings != null)
            {
                foreach (var s in GetSettings)
                {
                    getsettings.Add(s.Key, s.Value.GetConfigValue());
                }
            }

            var styles = new Dictionary<object, object>();
            if (Styles != null)
            {
                for (var i = 0; i < Styles.Count; i++)
                {
                    styles.Add(i, Styles[i].GetConfigValue());
                }
            }

            return new Dictionary<object, object>
            {
                {"version",Version},
                {"name",Name},
                {"fields",fields},
                {"getsetting",getsettings},
                {"style",styles}
            };
        }
        #endregion Methods
    }
}
